<?php

class SlApp extends MallbaseApp
{
	function index()
	{
		$db=&db();
		$k = empty($_REQUEST['k']) ? '' : $_REQUEST['k'];

		$conditions = 'isuse=1 and 1=1';
		if($k){
			$conditions.=" and (sname like '%".$k."%' or brief like '%".$k."%')";
		}

		$page = $this->_get_page(9);
		$sql="select * from zys_shippingline where $conditions order by sort_order limit $page[limit]";
		$ship=$db->getAll($sql);

		$sql="select count(1) from zys_shippingline where $conditions";
		$c=$db->getOne($sql);

		$page['item_count'] = $c;
		$this->_format_pageweb($page);
		$this->assign('page_info', $page);
			
		$this->assign('ship', $ship);
		$this->display('sl.html');
	}

	function ys(){
		$db=&db();
		$id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
		$sql="select * from zys_shippingline where sid=".$id." and isuse=1";
		$ship=$db->getRow($sql);

		$this->assign('s', $ship);

		$sql="select * from zys_dictionary where d_code='boxtype' and d_del=1 order by d_order";
		$type=$db->getAll($sql);

		$this->assign('type', $type);

		if($type){
			$this->assign('tt', $type[0]['d_value']);
		}

		$this->assign('d', time());
		$this->assign('d7', time()+7*24*60*60);
		$this->display('ys.html');
	}

	function ofast(){
		$db=&db();

		/*$id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);

		$sql="select * from zys_shippingline where sid=".$id." and isuse=1";
		$ship=$db->getRow($sql);

		if(empty($ship)){
		$sql="select * from zys_shippingline where isuse=1 limit 1";
		$ship=$db->getRow($sql);
		}
*/

		
		
		$sql="select * from zys_dictionary where d_code='boxtype' and d_del=1 order by d_order";
		$type=$db->getAll($sql);

		$this->assign('type', $type);

		if($type){
			$this->assign('tt', $type[0]['d_value']);
		}

		$this->assign('d', time());
		$this->assign('d7', time()+7*24*60*60);

		$sqldb=&sqldb();

		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);
		$sql="select depot,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		$u['user_name']=empty($u['user_name'])?'':$u['user_name'];
		$u['depot']=empty($u['depot'])?'':$u['depot'];
		
		$sql="select user_name,case real_name when '' then user_name else real_name end as real_name from zys_member where depot='".$u['depot']."'";
		$kf=$db->getAll($sql);
		$this->assign('kf', $kf);

		$name='';

		$pl=array();
		$sql="exec PT_WWW_Usrlog '".$u['user_name']."','P'";
		$temp=$sqldb->getAll($sql);
		if($temp){
			foreach ($temp as $v){
				if($v['USE_DD']){
					$v['USE_DD']=gmstr2time($v['USE_DD']);
				}
				$pl[]=$v;
			}
		}

		$gl=array();
		$sql="exec PT_WWW_Usrlog '".$u['user_name']."','J'";

		$temp=$sqldb->getAll($sql);
		if($temp){
			foreach ($temp as $v){
				
				if($v['USE_DD']){
					$v['USE_DD']=gmstr2time($v['USE_DD']);
				}
				$gl[]=$v;
			}
		}

		$sql="exec PT_WWW_Usrlog '".$u['user_name']."','N'";
		$name=$sqldb->getRowp($sql);
		$this->assign('n', $name['name']);
		$this->assign('cl', $cl);
		$this->assign('pl', $pl);
		$this->assign('gl', $gl);

		$this->display('ofast.html');
	}

	function pfast(){
		$db=&db();

		$sql="select * from zys_dictionary where d_code='boxtype' and d_del=1 order by d_order";
		$type=$db->getAll($sql);

		$this->assign('type', $type);

		if($type){
			$this->assign('tt', $type[0]['d_value']);
		}

		$this->assign('d', time());
		$this->assign('d7', time()+7*24*60*60);

		$sqldb=&sqldb();

		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);
		$sql="select depot,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		$name='';
		$sql="exec PT_WWW_Usrlog '".$u['user_name']."','N'";
		$name=$sqldb->getRowp($sql);
		
		$cl=array();
		$sql="exec PT_WWW_Usrlog '".$u['user_name']."','CJ'";

		$temp=$sqldb->getAll($sql);
		
		if($temp){
			foreach ($temp as $v){
				if($v['MK_DD']){
					$v['MK_DD']=gmstr2time($v['MK_DD']);
				}
				if($v['JH_DD']){
					$v['JH_DD']=gmstr2time($v['JH_DD']);
				}
				$cl[]=$v;
			}
		}

		$pl=array();
		$sql="exec PT_WWW_Usrlog '".$u['user_name']."','PJ'";
		$temp=$sqldb->getAll($sql);
		if($temp){
			foreach ($temp as $v){
			if($v['MK_DD']){
					$v['MK_DD']=gmstr2time($v['MK_DD']);
				}
				if($v['JH_DD']){
					$v['JH_DD']=gmstr2time($v['JH_DD']);
				}
				$pl[]=$v;
			}
		}

		$this->assign('n', $name['name']);
		$this->assign('cl', $cl);
		$this->assign('pl', $pl);

		$week=array(
		array('w'=>1,'n'=>'第一周','s'=>date('Y/m/d',strtotime('+1 week last monday')),'e'=>date('Y/m/d',strtotime('+1 week sunday'))),
		array('w'=>2,'n'=>'第二周','s'=>date('Y/m/d',strtotime('+2 week last monday')),'e'=>date('Y/m/d',strtotime('+2 week sunday'))),
		array('w'=>3,'n'=>'第三周','s'=>date('Y/m/d',strtotime('+3 week last monday')),'e'=>date('Y/m/d',strtotime('+3 week sunday'))),
		array('w'=>4,'n'=>'第四周','s'=>date('Y/m/d',strtotime('+4 week last monday')),'e'=>date('Y/m/d',strtotime('+4 week sunday'))),
		);

		$this->assign('B_DD', date('Y/m/d',strtotime('+1 week last monday')));
		$this->assign('E_DD', date('Y/m/d',strtotime('+1 week sunday')));
		$this->assign('week', $week);

		$this->display('pfast.html');
	}

	function pyd(){
		$sqldb=&sqldb();
		$CUS_NO = empty($_REQUEST['CUS_NO']) ? '' : $_REQUEST['CUS_NO'];
		$XX= empty($_REQUEST['XX']) ? '' : $_REQUEST['XX'];
		$QTY= empty($_REQUEST['QTY']) ? 0 : intval($_REQUEST['QTY']);

		$B_DD= empty($_REQUEST['B_DD']) ? '' : $_REQUEST['B_DD'];
		$E_DD =empty($_REQUEST['E_DD']) ? '' : $_REQUEST['E_DD'];
		$REM= empty($_REQUEST['REM']) ? '' : $_REQUEST['REM'];

		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);
		if(empty($uid)){
			$this->pons_error('info','您还没有登录，请先登录');
			exit();
		}

		$db=&db();
		$sql="select depot,del,company,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		if($u['del']){
			$this->pons_error('info','用户被禁用，如有疑问请联系客服');
			exit();
		}

		if(!$u['depot']){
			$this->pons_error('info','用户还没有被系统确认，无法预定');
			exit();
		}

		$sql="select * from T_SALM where SAL_NO='".$u['depot']."'";
		$salm=$sqldb->getRow($sql);

		if(!$salm){
			$this->pons_error('info','用户还没有被系统确认，无法预定');
			exit();
		}

		if(!$CUS_NO){
			$this->pons_error('info','船公司无效');
			exit();
		}

		$sql="select 1 from zys_shippingline where code='".$CUS_NO."'";
		$data=$db->getOne($sql);

		if(!$data){
			$this->pons_error('info','船公司无效');
			exit();
		}

		if(!$XX){
			$this->pons_error('info','请选择箱型');
			exit();
		}

		if(!$QTY){
			$this->pons_error('info','请输入箱量');
			exit();
		}

		if(!$B_DD){
			$this->pons_error('info','请选择正确的计划用箱周');
			exit();
		}
		if(!$E_DD){
			$this->pons_error('info','请选择正确的计划用箱周');
			exit();
		}
		$sql="insert into T_MF_UBOXPLAN(VC_NO,XX,QTY,B_DD,E_DD,REM,USR,COMP,COMP_NO,MK_DD,CUS_NO)
		values('".$this->create_guidnew()."','".$XX."','".$QTY."','".$B_DD."','".$E_DD."','".$REM."',
		'".$u['user_name']."','".$salm['COMP']."','".$salm['COMPNO']."',
		'".local_date('Y-m-d H:i:s',gmtime())."','".$CUS_NO."')";
		$sqldb->query($sql);

		$ret_url="index.php?app=sl&act=pfast";
		$this->pons_result($ret_url);
		exit();
	}


	function getsl(){
		$t = empty($_REQUEST['t']) ? '' : $_REQUEST['t'];
		$ty = empty($_REQUEST['ty']) ? 0 : intval($_REQUEST['ty']);
		
		$tt="";
		if($ty==2){
			$tt="isuse2=1";
		}
		elseif($ty){
			$tt="isuse1=1";
		}else{
			$tt="isuse=1";
		}
		
		$db=&db();
		$sql="select code,CONCAT(code,' ', sname) as sname from zys_shippingline where (code like '%".$t."%' or sname like '%".$t."%') and $tt order by code";
		$data=$db->getAll($sql);
		if($data){
			echo json_encode($data);
		}else{
			echo '[]';
		}
		exit();
	}

	function yd(){
		$sqldb=&sqldb();
		$CUS_NO = empty($_REQUEST['CUS_NO']) ? '' : $_REQUEST['CUS_NO'];
		$XX= empty($_REQUEST['XX']) ? '' : $_REQUEST['XX'];
		$QTY= empty($_REQUEST['QTY']) ? 0 : intval($_REQUEST['QTY']);
		$SHIP_OVOY= empty($_REQUEST['SHIP_OVOY']) ? '' : $_REQUEST['SHIP_OVOY'];
		$OVOY= empty($_REQUEST['OVOY']) ? '' : $_REQUEST['OVOY'];
		$TD_NO= empty($_REQUEST['TD_NO']) ? '' : $_REQUEST['TD_NO'];
		$USE_DD= empty($_REQUEST['USE_DD']) ? '' : $_REQUEST['USE_DD'];
		$EUSE_DD =empty($_REQUEST['EUSE_DD']) ? '' : $_REQUEST['EUSE_DD'];
		$REM= empty($_REQUEST['REM']) ? '' : $_REQUEST['REM'];
		$P_ADD= empty($_REQUEST['P_ADD']) ? '' : $_REQUEST['P_ADD'];
		$MK_USR= empty($_REQUEST['MK_USR']) ? '' : $_REQUEST['MK_USR'];

		$jj_flag= empty($_REQUEST['jj_flag']) ? '' : $_REQUEST['jj_flag'];


		$uid=$this->visitor->get("user_id");
		$uid=empty($uid)?0:intval($uid);
		if(empty($uid)){
			$this->pons_error('info','您还没有登录，请先登录');
			exit();
		}

		$db=&db();
		$sql="select depot,del,company,user_name from zys_member where user_id=".$uid;
		$u=$db->getRow($sql);

		if($u['del']){
			$this->pons_error('info','用户被禁用，如有疑问请联系客服');
			exit();
		}

		if(!$u['depot']){
			$this->pons_error('info','用户还没有被系统确认，无法预定');
			exit();
		}

		$sql="select * from T_SALM where SAL_NO='".$u['depot']."'";
		$salm=$sqldb->getRow($sql);

		if(!$salm){
			$this->pons_error('info','用户还没有被系统确认，无法预定');
			exit();
		}

		if(!$CUS_NO){
			$this->pons_error('info','船公司无效');
			exit();
		}

		$sql="select 1 from zys_shippingline where code='".$CUS_NO."'";
		$data=$db->getOne($sql);

		if(!$data){
			$this->pons_error('info','船公司无效');
			exit();
		}

		if(!$XX){
			$this->pons_error('info','请选择箱型');
			exit();
		}

		if(!$QTY){
			$this->pons_error('info','请输入箱量');
			exit();
		}

		if(!$SHIP_OVOY){
			$this->pons_error('info','请填写船名');
			exit();
		}

		if(!$OVOY){
			$this->pons_error('info','请填写航次');
			exit();
		}

		if(!$TD_NO){
			$this->pons_error('info','请填写提单号');
			exit();
		}

		if(!$USE_DD){
			//$this->pons_error('info','请选择用箱开始时间');
			//exit();
		}
		if(!$EUSE_DD){
			$this->pons_error('info','请选择最晚用箱时间');
			exit();
		}
		
		$MK_USR=empty($MK_USR)?$u['user_name']:$MK_USR;
		$sql="insert into T_UBOXPLAN(VC_NO,SHIP_NO,TD_NO,XX,QTY,USE_DD,REM,USR,COMP,COMP_NO,MK_DD,CUS_NO,OVOY,P_ADD,EUSE_DD,jj_flag,YW_TYPE,MK_USR)
		values('".$this->create_guidnew()."','".$SHIP_OVOY."','".$TD_NO."','".$XX."','".$QTY."','".$EUSE_DD."','".$REM."',
		'".$u['user_name']."','".$salm['COMP']."','".$salm['COMPNO']."',
		'".local_date('Y-m-d H:i:s',gmtime())."','".$CUS_NO."','".$OVOY."','".$P_ADD."','".$EUSE_DD."','".$jj_flag."',7,'".$MK_USR."')";
		$sqldb->query($sql);

		$ret_url="index.php?app=plan";
		$this->pons_result($ret_url);
		exit();
	}

	function cancel(){
		$sqldb=&sqldb();
		$n= empty($_REQUEST['n']) ? '' : $_REQUEST['n'];
		if(empty($n)){
			$this->pons_error('info','要箱计划不存在');
			exit();
		}
		$u=$this->visitor->get("user_name");
		$sql="exec pt_uboxcheck '".$n."','".$u."'";
		$d=$sqldb->getRow($sql);

		if(empty($d)){
			$this->pons_error('info','操作失败,请重试');
			exit();
		}else{
			if($d['flag']=='F'){
				$this->pons_error('info',$d['ts']);
				exit();
			}
		}

		$this->pons_result('index.php?app=plan');
		exit();
	}
}

?>
